/**
 * api list
 */
import AppConfig from './index.js';
import { request } from '../utils/index.js';

const host = AppConfig.server.host
const RootUrl = `${host}/v1`

export const Api = {
  host: `${RootUrl}`,
  // Auth
  login: `${RootUrl}/public/login`,
  register: `${RootUrl}/public/rigister`,
  getMsgCode: `${RootUrl}/public/sendSme`,
  getInviteCode: `${RootUrl}/public/hashcode`,
  getOpenid: `${RootUrl}/public/openid`, //获取openid
 updataPass: `${RootUrl}/public/fixPwd`,


// index

getSwipeList: `${RootUrl}/pt/bannerList`,
  // cart
  getMyCart: `${RootUrl}/goods/myCart`,
  addCart: `${RootUrl}/goods/addCart`,
  editCart: `${RootUrl}/goods/editCart`,
  delCart: `${RootUrl}/goods/delCart`,
   cartAll: `${RootUrl}/goods/cartAll`,



  // 领养商品详情
  getGoodDetails: `${RootUrl}/goods/goodsDetails`,

  // 千禧学堂
  qxSchoolIndex: `${RootUrl}/shool/index`,

  // 商城
  goodListType: {
    RECOMMAND: '1',
    VIP: '2',
    GIFT_CARD_ZONE: '3',
    GIFT_CARD: '4,12',
    GIFT_CARD_SHOP: '5',
    ADOPT: '6',
    SHEEP_MEET_STORE: '7',
    EXCHANGE_RED_WINE: '8'
  },

//   商品列表
  getGoodList: `${RootUrl}/goods/lists`,
 //商品立即购买 去结算按钮
  order: `${RootUrl}/goods/order`,
//单个 运费
  confirmOrder: `${RootUrl}/goods/confirmOrder`,
// 多个商品运费
  confirmCartAll: `${RootUrl}/goods/confirmCartAll`,
  // 地址
  addNewAddress: `${RootUrl}/Address/addAddress`,
 getAddressList: `${RootUrl}/address/addressList`,
 delAddress: `${RootUrl}/Address/delAddress`,
   //设置默认地址
   tacitly: `${RootUrl}/address/setaddress`,

  // 获取支付参数
  getWXPayOptions: `${RootUrl}/goods/goodsPay`,
  //获取羊贝
  userAssets: `${RootUrl}/user/userAssets`,
  
//   获取抽奖列表
 getlucklist: `${RootUrl}/goods/luck_list`,
 getluck: `${RootUrl}/goods/luck`,



// 羊币
  xfCoin: `${RootUrl}/user/xfCoin`,
//领养
sheepOrder:`${RootUrl}/sleep/sheepOrder`,
sheepSite:`${RootUrl}/sleep/sheepSite`,

  sheep_coin: `${RootUrl}/user/sheep_coin`,
//   抵扣羊币
 paySleepCoin: `${RootUrl}/user/paySleepCoin`,

//   接收礼品卡
 accept: `${RootUrl}/goods/accept`,
//  我的礼品卡
 giftCart: `${RootUrl}/goods/giftCart`,

//  兑换礼品卡
exchange: `${RootUrl}/goods/exchange`,


//  今日签到
 sign: `${RootUrl}/user/sign`,
 //羊贝抵扣
 sheepB:`${RootUrl}/sleep/sheepB`
};


// 支付

/**
 * promise 形式的 小程序的微信支付
 * @param {object} pay 支付所必要的参数
 */
export const requestPayment = (pay) => {
  console.log(pay)
    return new Promise((resolve, reject) => {
        wx.requestPayment({
            ...pay,
            success: (result) => {
                console.log("--------支付成功")
                resolve(result)
            },
            fail: (err) => {
                console.log("--------支付失败")
                reject(err);
            }
        });

    })
}








// 领养商品详情
export const getGoodDetail = (goodid) => {
  return new Promise((resolve, reject) => {
    request({
      url: Api.getGoodDetails,
      data: { id: goodid },
      method: 'POST'
    })
      .then(res => {
        resolve(res.data.data)
      })
      .catch(err => {
        reject(err)
      });
  })
}


// 加入购物车
export const addCartGood = (goodid) => {
  return new Promise((resolve, reject) => {
    request({
      url: Api.addCart,
      data: { goods_id: goodid },
      method: 'POST'
    }).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}

export const delCartGood = (goodid) => {
  return new Promise((resolve, reject) => {
    request({
      url: Api.delCart,
      method: 'POST',
      data: {
        id: goodid
      }
    }).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}

export const editCartCount = (goodid, num) => {
  return new Promise((resolve, reject) => {
    request({
      url: Api.editCart,
      method: 'POST',
      data: {
        id: goodid,
        num: num
      }
    }).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}

export const getMyAddressList = () => {
  return new Promise((resolve, reject) => {
    request({ url: Api.getAddressList }).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}

export const createNewAddress = (form) => {
  return new Promise((resolve, reject) => {
    request({ url: Api.addNewAddress, method: 'POST', data: form }).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}

export const getWXPayOptions = (data) => {
  return new Promise((resolve, reject) => {
    request({ url: Api.getWXPayOptions, method: 'POST', data }).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}
